package wiinf;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JComboBox;

/**
 *
 * @author Randy van Heijst & David Roos
 */
public class serverViewModel extends javax.swing.JFrame {
    private afdToevoegen afdtoevoegen;
    private afdBewerken afdbewerken;
    private afdVerwijderen afdverwijderen;
    private oplToevoegen opltoevoegen;
    private oplBewerken oplbewerken;
    private oplVerwijderen oplverwijderen;
    protected serverController server;
    private Color c1 = Color.BLACK;
    private Color c2 = Color.BLACK;
    private Color c3 = Color.BLACK;

    /**
     * Aanmaken van een nieuwe JFrame
     */
    public serverViewModel() {
        initComponents();
        server = new serverController(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Menu = new javax.swing.JPanel();
        BafdToevoegen = new javax.swing.JButton();
        BafdBewerken = new javax.swing.JButton();
        BafdVerwijderen = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        BoplToevoegen = new javax.swing.JButton();
        BoplBewerken = new javax.swing.JButton();
        BoplVerwijderen = new javax.swing.JButton();
        MainPanel = new javax.swing.JPanel();
        ServerPanel = new javax.swing.JPanel();
        Bserver = new javax.swing.JButton();
        L1 = new javax.swing.JLabel();
        L2 = new javax.swing.JLabel();
        L3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        BafdToevoegen.setText("Toevoegen");
        BafdToevoegen.setMaximumSize(new java.awt.Dimension(89, 23));
        BafdToevoegen.setMinimumSize(new java.awt.Dimension(89, 23));
        BafdToevoegen.setPreferredSize(new java.awt.Dimension(89, 23));
        BafdToevoegen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BafdToevoegenActionPerformed(evt);
            }
        });

        BafdBewerken.setText("Bewerken");
        BafdBewerken.setMaximumSize(new java.awt.Dimension(105, 23));
        BafdBewerken.setMinimumSize(new java.awt.Dimension(89, 23));
        BafdBewerken.setPreferredSize(new java.awt.Dimension(105, 23));
        BafdBewerken.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BafdBewerkenActionPerformed(evt);
            }
        });

        BafdVerwijderen.setText("Verwijderen");
        BafdVerwijderen.setPreferredSize(new java.awt.Dimension(105, 23));
        BafdVerwijderen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BafdVerwijderenActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 14));
        jLabel1.setText("Afdeling");

        jLabel2.setFont(new java.awt.Font("Verdana", 1, 14));
        jLabel2.setText("Opleiding");

        BoplToevoegen.setText("Toevoegen");
        BoplToevoegen.setMaximumSize(new java.awt.Dimension(89, 23));
        BoplToevoegen.setMinimumSize(new java.awt.Dimension(89, 23));
        BoplToevoegen.setPreferredSize(new java.awt.Dimension(105, 23));
        BoplToevoegen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BoplToevoegenActionPerformed(evt);
            }
        });

        BoplBewerken.setText("Bewerken");
        BoplBewerken.setMaximumSize(new java.awt.Dimension(89, 23));
        BoplBewerken.setMinimumSize(new java.awt.Dimension(89, 23));
        BoplBewerken.setPreferredSize(new java.awt.Dimension(105, 23));
        BoplBewerken.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BoplBewerkenActionPerformed(evt);
            }
        });

        BoplVerwijderen.setText("Verwijderen");
        BoplVerwijderen.setPreferredSize(new java.awt.Dimension(105, 23));
        BoplVerwijderen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BoplVerwijderenActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout MenuLayout = new javax.swing.GroupLayout(Menu);
        Menu.setLayout(MenuLayout);
        MenuLayout.setHorizontalGroup(
            MenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MenuLayout.createSequentialGroup()
                .addGroup(MenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(MenuLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addComponent(BafdVerwijderen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(MenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(BafdBewerken, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(BafdToevoegen, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(MenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(BoplBewerken, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(BoplToevoegen, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(BoplVerwijderen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(MenuLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        MenuLayout.setVerticalGroup(
            MenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MenuLayout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BafdToevoegen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BafdBewerken, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BafdVerwijderen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(4, 4, 4)
                .addComponent(BoplToevoegen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BoplBewerken, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BoplVerwijderen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        MainPanel.setPreferredSize(new java.awt.Dimension(321, 209));
        MainPanel.setLayout(new java.awt.BorderLayout());

        Bserver.setText("Start server");
        Bserver.setPreferredSize(new java.awt.Dimension(105, 23));
        Bserver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BserverActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ServerPanelLayout = new javax.swing.GroupLayout(ServerPanel);
        ServerPanel.setLayout(ServerPanelLayout);
        ServerPanelLayout.setHorizontalGroup(
            ServerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ServerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Bserver, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48)
                .addGroup(ServerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(L3, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                    .addComponent(L2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                    .addComponent(L1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE))
                .addContainerGap())
        );
        ServerPanelLayout.setVerticalGroup(
            ServerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ServerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ServerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ServerPanelLayout.createSequentialGroup()
                        .addComponent(L1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(L2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(L3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(Bserver, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(Menu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(MainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ServerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Menu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(MainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ServerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Actionlistener horende bij de afdeling bewerken button
     *
     * @param evt action event
     */
    private void BafdBewerkenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BafdBewerkenActionPerformed
        afdbewerken = new afdBewerken(this);
        setPanel(afdbewerken);    
    }//GEN-LAST:event_BafdBewerkenActionPerformed

    /**
     * Actionlistener horende bij de afdeling toevoegen button
     *
     * @param evt action event
     */
    private void BafdToevoegenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BafdToevoegenActionPerformed
        afdtoevoegen = new afdToevoegen(this);
        setPanel(afdtoevoegen);
    }//GEN-LAST:event_BafdToevoegenActionPerformed

    /**
     * Actionlistener horende bij de afdeling verwijderen button
     *
     * @param evt action event
     */
    private void BafdVerwijderenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BafdVerwijderenActionPerformed
        afdverwijderen = new afdVerwijderen(this);
        setPanel(afdverwijderen);
    }//GEN-LAST:event_BafdVerwijderenActionPerformed

    /**
     * Actionlistener horende bij de opleiding toevoegen button
     *
     * @param evt action event
     */
    private void BoplToevoegenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BoplToevoegenActionPerformed
        opltoevoegen = new oplToevoegen(this);
        setPanel(opltoevoegen);
    }//GEN-LAST:event_BoplToevoegenActionPerformed

    /**
     * Actionlistener horende bij de opleiding bewerken button
     *
     * @param evt action event
     */
    private void BoplBewerkenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BoplBewerkenActionPerformed
        oplbewerken = new oplBewerken(this);
        setPanel(oplbewerken);
    }//GEN-LAST:event_BoplBewerkenActionPerformed

    /**
     * Actionlistener horende bij de opleiding verwijderen button
     *
     * @param evt action event
     */
    private void BoplVerwijderenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BoplVerwijderenActionPerformed
        oplverwijderen = new oplVerwijderen(this);
        setPanel(oplverwijderen);
    }//GEN-LAST:event_BoplVerwijderenActionPerformed

    /**
     * Actionlistener horende bij de server starten button
     *
     * @param evt action event
     */
    private void BserverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BserverActionPerformed
        if (Bserver.getText().equals("Start server")) {
            server.fh.createMainXML();

            setMessage("Server gestart", 1);
            try {
                //server.serverListener();
                server.serverSender();
                Bserver.setText("Stop server");
            } catch (Exception ex) {
                System.out.println(ex);
            }
        } else if (Bserver.getText().equals("Stop server")) {
            server.stopThread();
            setMessage("Server gestopt", 1);
            Bserver.setText("Start server");
        }

    }//GEN-LAST:event_BserverActionPerformed

    /**
     * voegt de panel toe aan de JFrame
     *
     * @param o De toe te voegen panel
     */
    public void setPanel(Component o) {
        MainPanel.removeAll();
        MainPanel.add(o, BorderLayout.EAST);
        MainPanel.validate();
    }

    /**
     * Het vullen van de combo box met opleidingen - wordt aangeroepen vanuit de losse panels
     *
     * @param c De combo box die gevuld moet worden
     */
    public void setAfdelingBox(JComboBox c) {
        String[] f = server.fh.getMap();
        for(int i =0; i < f.length; i++) {
           c.addItem(f[i]);
        }
    }

    /**
     * Het plaatsen van status berichten in de user interface
     *
     * @param message Het status bericht
     * @param messageType De kleur van het bericht; 1 = zwart, 2 = rood, 3 = groen
     */
    public void setMessage(String message, int messageType) {
        c3 = c2;
        c2 = c1;
        
        switch(messageType) {
            case 1: c1 = Color.BLACK; break;
            case 2: c1 = Color.RED; break;
            case 3: c1 = Color.GREEN; break;
        }

        L3.setText(L2.getText());
        L2.setText(L1.getText());
        L1.setText(message);

        L1.setForeground(c1);
        L2.setForeground(c2);
        L3.setForeground(c3);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new serverViewModel().setVisible(true);
            }
        });
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BafdBewerken;
    private javax.swing.JButton BafdToevoegen;
    private javax.swing.JButton BafdVerwijderen;
    private javax.swing.JButton BoplBewerken;
    private javax.swing.JButton BoplToevoegen;
    private javax.swing.JButton BoplVerwijderen;
    private javax.swing.JButton Bserver;
    private javax.swing.JLabel L1;
    private javax.swing.JLabel L2;
    private javax.swing.JLabel L3;
    private javax.swing.JPanel MainPanel;
    private javax.swing.JPanel Menu;
    private javax.swing.JPanel ServerPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables



}
